#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：9.汽车之家异步案例.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-12-17 15:09
'''
import redis
import chardet # 判断页面编码的，需要大家使用pip自行安装
import hashlib
import asyncio
import aiohttp
import aiomysql
from lxml import etree

class CarSpider:
    redis_cliect = redis.Redis()

    def __init__(self):
        self.url = "https://www.che168.com/china/a0_0msdgscncgpi1ltocsp{}exf4x0/?pvareaid=102179#currengpostion"
        self.api_url = "https://cacheapigo.che168.com/CarProduct/GetParam.ashx?specid={}"
        self.headers = {
            "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.50"
        }

    def __del__(self):
        print("redis数据库即将关闭.....")
        self.redis_cliect.close()

    # 获取汽车id
    async def get_car_id(self, page, session, pool):
        """
        :param page: 分页参数
        :param session: aiohttp请求对象
        :param pool: 数据库连接池
        :return:
        """
        async with session.get(self.url.format(page), headers=self.headers) as response:
            coontent = await response.read()
            encoding = chardet.detect(coontent)['encoding']
            if encoding == 'GB2312' or encoding == 'ISO-8859-1':
                result = coontent.decode("gbk")
            else:
                result = coontent.decode(encoding)
                print("被反爬了....")
            tree = etree.HTML(result)

            id_list = tree.XPAth

    async def main(self):
        async with aiohttp.ClientSession() as session:
            tasks = [loop.create_task(self.get_car_id(page, session, None)) for page in range(1,101)]
            await asyncio.wait(tasks)


if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    car_spider = CarSpider()
    loop.run_until_complete(car_spider.main())


